import { createStore } from 'vuex';
import api from '@/services/api'; // 导入 api 对象

export default createStore({
  state: {
    users: [],
    devices: [],
    sensorData: [],
  },
  mutations: {
    setUsers(state, users) {
      state.users = users;
    },
    setDevices(state, devices) {
      state.devices = devices;
    },
    setSensorData(state, sensorData) {
      state.sensorData = sensorData;
    },
  },
  actions: {
    async fetchUsers({ commit }) {
      try {
        const response = await api.getUsers(); // 调用 api.getUsers()
        commit('setUsers', response.data);
      } catch (error) {
        console.error('获取用户数据失败:', error);
      }
    },
    async fetchDevices({ commit }) {
      try {
        const response = await api.getDevices(); // 调用 api.getDevices()
        commit('setDevices', response.data);
      } catch (error) {
        console.error('获取设备数据失败:', error);
      }
    },
    async fetchSensorData({ commit }) {
      try {
        const response = await api.getSensorData(); // 调用 api.getSensorData()
        commit('setSensorData', response.data);
      } catch (error) {
        console.error('获取传感器数据失败:', error);
      }
    },
  },
});